/*@@

    Copyright (c) 2010 BlueSky LTD.  All rights reserved. 

    Created_datetime : 2010-6-23 17:18:31

    File Name :	base_mutex.cpp

    Author : Wang Hu (ghoststears@gmail.com)

    Description : 

@@*/


#include "base/base_mutex.h"


namespace BLK
{
    Mutex::Mutex()
    {
#ifdef _WIN32
        m_mutex = ::CreateMutex(NULL, FALSE, NULL);
#else
        pthread_mutex_init(&m_mutex, NULL);
#endif
    }

    Mutex::~Mutex()
    {
#ifdef _WIN32
        ::CloseHandle(m_mutex);
#else
        pthread_mutex_destroy(&m_mutex);
#endif
    }

    bool Mutex::Lock()
    {
#ifdef _WIN32
        if (WAIT_FAILED == ::WaitForSingleObject(m_mutex, INFINITE))
        {
            return false;
        }
#else
        pthread_mutex_lock(&m_mutex);
#endif

        return true;
    }

    bool Mutex::Unlock() 
    {
#ifdef _WIN32
        if (!::ReleaseMutex(m_mutex))
        {
            return false;
        }
#else
        pthread_mutex_unlock(&m_mutex);
#endif
        return true;
    }
};
